package com.carmanagement



import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

import org.springframework.security.access.annotation.Secured


@Secured(['ROLE_ADMIN', 'ROLE_USER'])
@Transactional
class FullTankController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def show(FullTank fullTankInstance) {
	respond fullTankInstance
    }

    def create() {
		def fullTankInstance = new FullTank(params)
		def vehicleInstance = Vehicle.findById(params.vehicleId)
		if(!vehicleInstance){
		    noVehicleFound()
		    return
		}
		fullTankInstance.vehicle = vehicleInstance
		respond fullTankInstance
    }

    @Transactional
    def save(FullTank fullTankInstance) {
		if (fullTankInstance == null) {
		    notFound()
		    return
		}

		if(params.date){
			fullTankInstance.date = Date.parse("yyyy/MM/dd", params.date)
			fullTankInstance.validate()
		}

		if (fullTankInstance.hasErrors()) {
		    respond fullTankInstance.errors, view:'create'
		    return
		}

		fullTankInstance.save(flush:true, failOnError: true)
		request.withFormat {
		    form {
			flash.message = message(code: 'default.created.message', args: [
			    message(code: 'fullTankInstance.label', default: 'FullTank'),
			    fullTankInstance.id
			])
			redirect(controller:"vehicle", action:"show", id:fullTankInstance.vehicle.id)
		    }
		    '*' { respond fullTankInstance, [status: CREATED] }
		}
    }

    def edit(FullTank fullTankInstance) {
		respond fullTankInstance
    }

    @Transactional
    def update(FullTank fullTankInstance) {
		if (fullTankInstance == null) {
		    notFound()
		    return
		}
		fullTankInstance.clearErrors()
		Date date = params.date("date", "yyyy/MM/dd")
		fullTankInstance.date = date
		if (fullTankInstance.hasErrors()) {
		    respond fullTankInstance.errors, view:'edit'
		    return
		}

		fullTankInstance.save flush:true

		request.withFormat {
		    form {
			flash.message = message(code: 'default.updated.message', args: [
			    message(code: 'FullTank.label', default: 'FullTank'),
			    fullTankInstance.id
			])
			redirect fullTankInstance
		    }
		    '*'{ respond fullTankInstance, [status: OK] }
		}
    }

    @Transactional
    def delete(FullTank fullTankInstance) {

		if (fullTankInstance == null) {
		    notFound()
		    return
		}

		fullTankInstance.delete flush:true

		request.withFormat {
		    form {
			flash.message = message(code: 'default.deleted.message', args: [
			    message(code: 'FullTank.label', default: 'FullTank'),
			    fullTankInstance.id
			])
			redirect (controller:"vehicle", action:"show", id:fullTankInstance.vehicle.id)
		    }
		    '*'{ render status: NO_CONTENT }
		}
    }

    protected void notFound() {
		request.withFormat {
		    form {
			flash.message = message(code: 'default.not.found.message', args: [
			    message(code: 'fullTankInstance.label', default: 'FullTank'),
			    params.id
			])
			redirect controller: "vehicle", action: "index", method: "GET"
		    }
		    '*'{ render status: NOT_FOUND }
		}
    }

    protected void noVehicleFound() {
		request.withFormat {
		    form {
			flash.message = message(code: 'default.not.found.message', args: [
			    message(code: 'vehicleInstance.label', default: 'Vehicle'),
			    params.vehicleId
			])
			redirect controller: "vehicle", action: "index", method: "GET"
		    }
		    '*'{ render status: NOT_FOUND }
		}
    }
}
